package com.qqtech.qquan.user.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.user.enums.UserWithdrawStatusDictEnum;
import com.qqtech.qquan.user.model.UserWithdraw;
import com.qqtech.qquan.user.model.UserWithdrawQuery;
import com.qqtech.qquan.user.model.UserWithdrawVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserMoneyLogService;
import com.qqtech.qquan.user.service.UserWithdrawService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/withdraw")
public class UserWithdrawController extends BaseWebController<UserWithdraw, UserWithdrawQuery> {
	@Resource
	private UserWithdrawService userWithdrawService;

	@Resource
	private UserMoneyLogService userMoneyLogService;

	@Resource
	private UserBaseService userBaseService;

	@Override
	protected BaseService<UserWithdraw> getBaseService() {
		return userWithdrawService;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody UserWithdraw userWithdraw) {
		Result result = new Result();
		double validMoney = userMoneyLogService.getValidMoney(getUserId());
		if (userWithdraw == null) {
			result.addErr("参数不能为空");
			return result;
		} else {
			if (userWithdraw.getCash().equals(BigDecimal.ZERO)) {
				result.addErr("提现金额不能为零。");
				return result;
			} else if (userWithdraw.getCash().compareTo(BigDecimal.ZERO) < 0) {
				result.addErr("提现金额不能为负数。");
				return result;
			}

		}
		if (userWithdraw.getCash().compareTo(BigDecimal.valueOf(validMoney)) == 1) {// 大于可提取的金额
			result.addErr("超过可提取最大提现金额。");
			return result;
		}
		userWithdraw.setUserId(getUserId());
		userWithdraw.setStatus(UserWithdrawStatusDictEnum.APPLYING.getCode());
		userWithdrawService.insert(userWithdraw);
		result.put("withdrawCash", userMoneyLogService.getValidMoney(getUserId()));
		return result;
	}

	/**
	 * 管理列表
	 * 
	 * @param UserWithdrawQuery
	 * @return
	 */
	@RequestMapping(value = "/managePage")
	@ResponseBody
	public Result managePage(@RequestBody UserWithdrawQuery userWithdrawQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		userWithdrawQuery.setUserId(curUserId);
		Pageable pageKit = new PageKit(userWithdrawQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<UserWithdrawVo> page = userWithdrawService.queryPageList(userWithdrawQuery, pageKit);
		result.put("page", page);
		return result;
	}
}
